#include "Widget.h"
#include "ui_widget.h"
#include <QPainter>
#include <QFileDialog>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    mypainter = new QPainter;
    gameMap = new GameMap(this);
    QString filename = QFileDialog::getOpenFileName(this,"选择地图文件","./","*.txt");
    gameMap->iniByTxt(filename);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::DrawMap()
{
    for(int i=0;i<gameMap->row;i++)
    {
        for(int j=0;j<gameMap->col;j++)
        {
            QString imageUrl;
            switch(gameMap->Gmap[i][j])
            {
            case road:imageUrl=":/map_image/element/road.png";break;
            case wall:imageUrl=":/map_image/element/wall.gif";break;
            case grass:imageUrl=":/map_image/element/grass.gif";break;
            }
            QImage image(imageUrl);
            mypainter->drawImage(QRectF(j*image.width(),i*image.height(),image.width(),image.height()),image);
        }
    }
}

void Widget::paintEvent(QPaintEvent* event)
{
    Q_UNUSED(event);
    mypainter->begin(this);
    DrawMap();

    mypainter->end();
}

